package com.sailorj.service.impl;

import com.sailorj.entity.TbRepair;
import com.sailorj.mapper.TbRepairMapper;
import com.sailorj.service.TbRepairService;
import com.sailorj.util.PageBean;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbRepairServiceImpl implements TbRepairService {

    @Autowired
    public TbRepairMapper tbRepairMapper;


    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addRepair(TbRepair tbRepair) {
        tbRepairMapper.insert(tbRepair);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void updateRepairById(TbRepair tbRepair) {
        tbRepairMapper.updateByPrimaryKey(tbRepair);
    }



    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbRepair getRepairbById(int id) {
        return tbRepairMapper.selectByPrimaryKey(id);
    }

//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    public List<TbRepair> getRepairList() {
//        return tbRepairMapper.selectRepairList();
//    }
    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getRepairList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbRepair> data = tbRepairMapper.selectRepairList(page,size);
        Long total=tbRepairMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

    // 导出excelList
    @Override
    public XSSFWorkbook excelList() {
        List<TbRepair> list = tbRepairMapper.getRepairList();//查出数据库数据
        XSSFWorkbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("Goods");//创建一张表
        Row titleRow = sheet.createRow(0);//创建第一行，起始为0
        titleRow.createCell(0).setCellValue("序号");//第一列
        titleRow.createCell(1).setCellValue("设备名称");
        titleRow.createCell(2).setCellValue("设备编号");
        titleRow.createCell(3).setCellValue("型号");
        titleRow.createCell(4).setCellValue("故障描述");
        titleRow.createCell(5).setCellValue("报修时间");
        titleRow.createCell(6).setCellValue("报修人");
        int cell = 1;
        for (TbRepair goods : list) {
            Row row = sheet.createRow(cell);//从第二行开始保存数据
            row.createCell(0).setCellValue(cell);
            row.createCell(1).setCellValue(goods.getDeviceName());//将数据库的数据遍历出来
            row.createCell(2).setCellValue(goods.getDeviceId());
            row.createCell(3).setCellValue(goods.getModelId());
            row.createCell(4).setCellValue(goods.getContent());
            row.createCell(5).setCellValue(goods.getCreatedTime());
            row.createCell(6).setCellValue(goods.getUserName());
            cell++;
        }
        return wb;
    }

}
